﻿using System;
using System.Collections.Generic;
using System.Text;
using net.veekee.common;
using net.veekee.common.data;
using net.veekee.common.designpattern;

namespace net.veekee.logprocessor.plugin.context
{
    public class ProcessorContext : Singleton<ProcessorContext>
    {
        public static readonly String DefaultOutputFilename = "default";

        private String input = null;
        public String Input
        {
            get
            {
                return this.input;
            }
        }

        private CollectionManager<long> shortoutput = null;
        public CollectionManager<long> AvailableShortOutputs
        {
            get
            {
                return shortoutput;
            }
        }
        private Dictionary<String, FileContext> output = null;
        public String[] AvailableOutputs
        {
            get
            {
                String[] list = new String[this.output.Keys.Count];
                this.output.Keys.CopyTo(list, 0);
                return list;
            }
        }

        private ContextEvent evt;
        public ContextEvent Event
        {
            get
            {
                return this.evt;
            }
        }

        public ProcessorContext()
        {
            this.shortoutput = new CollectionManager<long>();
            this.output = new Dictionary<String, FileContext>();
        }

        public void SetInput(String inputfilename)
        {
            if (inputfilename == null)
                return;
            Log.GetInstance().Trace("Setting input file as '{0}'", inputfilename);
            this.input = inputfilename;
        }

        public void SetEvent(ContextEvent _evt)
        {
            this.evt = _evt;
        }

        public void AddOutput(String idx, String val)
        {
            if (val == null)
                return;
            if (val.Length == 0)
                return;
            Log.GetInstance().Trace("Setting {0} output file as '{1}'", idx, val);
            if (! this.output.ContainsKey(idx))
            {
                this.output.Add(idx, new FileContext(val));
            }
        }

        public void RenameOutputs()
        {
            foreach (KeyValuePair<String, FileContext> o in this.output)
            {
                o.Value.Rename();
            }
        }

        private void RemoveOutput(String idx)
        {
            Log.GetInstance().Trace("Removing {0} output file", idx);
            if (this.output.ContainsKey(idx))
                this.output.Remove(idx);
        }

        public FileContext GetOutput(String idx)
        {
            if (this.output.ContainsKey(idx))
                return this.output[idx];
            return null;
        }
    }

    public enum ContextEvent
    {
        None,
        OnInit,
        OnStart,
        OnNewItem,
        OnNewDiscardedItem,
        OnValidatedItem,
        OnFinish,
        OnFinalize
    }
}
